<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
</head>
<body>
    <div id="app">
        <reg></reg>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        Vue.component("input-element",{
            props:["type","info","value"],
            template: `
                <div>
                    <label><slot></slot></label>
                    <input :type="type"
                     :value="value"
                     @input="$emit('input',$event.target.value)"
                     @blur="$emit('validate',$event)" />
                    <span>{{info}}</span>
                </div>
            `
        });
        Vue.component("reg",{
            template: `
                <div>
                    value:{{username.value}}
                    <h1>注册</h1>
                    <input-element
                     type="text"
                     :info="username.info"
                     @validate="validateUsername"
                     v-model="username.value"
                     >用户名：</input-element>
                    <input-element
                     v-model="pwd.value"
                    type="password">密码：</input-element>
                    <input-element
                     v-model="confirm.value"
                    type="password">确认密码：</input-element>
                    <button type="button" @click="handleReg">注册</button>
                </div>
            `,
            data(){
                return {
                    username:{
                        value:"",
                        info:""
                    },
                    pwd:{
                        value:""
                    },
                    confirm:{
                        value:""
                    }
                }
            },
            methods:{
                validateUsername(e){
                    console.log(e.target.value);
                    if(/^\w{6,20}$/.test(e.target.value)){
                        this.username.info = "√";
                    }else{
                        this.username.info = "X";
                    }
                },
                handleReg(){
                    console.log(this.username.value,this.pwd.value,this.confirm.value);
                }
            }
        });
        const app = new Vue({
            el: "#app",
            data:{
                obj:{}
            }
        });
    </script>
    
</body>
</html>